﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using IEstate.Core.Models;
using IEstate.Repositories.Database;
using IEstate.Web.Models;

namespace IEstate.Web.Controllers
{
    [Authorize]
    public class UserController : Controller
    {
        private ApplicationDbContext DB = new ApplicationDbContext();
        private EstateContext DBContext = new EstateContext();

        public ActionResult Index()
        {
            var user = User.Identity.GetUserId();
            var result = DB.Users.FirstOrDefault(p => p.Id == user);

            ViewBag.Counter = 0;

            IEnumerable<Estate> query = DBContext.Estates.Where(p => p.UID == result.userCode);

            return View("Index", query);
        }

        [HttpGet]
        public ActionResult Edit(int id = 1)
        {
            var user = User.Identity.GetUserId();
            var result = DB.Users.FirstOrDefault(p => p.Id == user);

            var howMany = DBContext.Estates.Count(p => p.UID == result.userCode);

            if (id > howMany || id < 1) {
                ViewBag.Error = true;
                return View("Edit");
            }

            int query;
            if (id > 1)
                query = DBContext.Estates.Where(p => p.UID == result.userCode).OrderBy(p => p.EstateID).Skip(id - 1).Select(p => p.EstateID).First();
            else
                query = DBContext.Estates.Where(p => p.UID == result.userCode).Select(p => p.EstateID).First();

            ViewBag.query = query;

            return View("Edit", new EstateViewModel());
        }

        [HttpPost]
        public ActionResult Edit(int id, EstateViewModel estateView, HttpPostedFileBase image)
        {
            // Skip model validation because in case of only one field update
            // User must supply every field a value

            if(id < estateView.HowMany || id < 1)
            {
                ViewBag.Error = true;
                return View("Edit");
            }

            Estate estate = DBContext.Estates.FirstOrDefault (p => p.EstateID == (int)estateView.SecretID);

            int value = DBContext.EImages.Count(p => p.EstateID == (int)estateView.SecretID);
            ViewBag.value = value + 1;

            if (estateView.Street != null) estate.Street = estateView.Street;
            if (estateView.Bedrooms != null) estate.Bedrooms = (int)estateView.Bedrooms;
            if (estateView.Price != null) estate.Price = (int)estateView.Price;
            if (estateView.Size != null) estate.Size = (int)estateView.Size;
            if (estateView.Info != null) estate.Info = estateView.Info;

            EImage Image = new EImage();

            if(image != null)
            {
                Image.EstateID = estate.EstateID;
                Image.ImageMimeType = image.ContentType;
                Image.ImageData = new byte[image.ContentLength];
                image.InputStream.Read(Image.ImageData, 0, image.ContentLength);
                DBContext.EImages.Add(Image);
            }

            DBContext.SaveChanges();
            ViewBag.Id = (int)estateView.SecretID;

            if (image == null) ViewBag.image = true;

            return View("_Edit", estate);
        }
	}
}